package com.sgai.zero_robot.ui.alarm;

import static com.sgai.zero_robot.utils.LogUtil.rootPath;
import android.os.HandlerThread;
import android.os.Looper;
import com.sgai.zero_robot.pojo.AlarmInfo;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Describe :
 * Created by zzw on 2024/8/28
 */
public class AlarmUtils {
    private static AlarmHandler handler;
    public static void init(){
        //防止多次初始化
        if (handler!=null){
            return;
        }
        HandlerThread handlerThread = new HandlerThread("log");
        handlerThread.start();
        Looper looper = handlerThread.getLooper();
        handler = new AlarmHandler(looper);
        new Thread(new Runnable(){
            @Override
            public void run() {
                File file = new File(rootPath+"/alarm/");
                if (!file.exists()){
                    file.mkdirs();
                }else if (!file.isDirectory()){
                    file.delete();
                    file.mkdirs();
                }
                File[] files = file.listFiles();
                if (files == null||files.length<=0){
                    String path = rootPath+"/alarm/" + logInfoFormat.format(new Date(System.currentTimeMillis())) + ".txt";
                    if (handler!=null){
                        handler.setPath(path);
                    }
                }else{
                    if (files.length > 7){
                        int i = files.length - 7;
                        if (i>0){
                            for (int j = 0;j<i;j++){
                                files[j].delete();
                            }
                        }
                    }
                    files = file.listFiles();
                    if (files.length == 7){
                        if (files[files.length-1].length()>2*1024*1024){
                            files[0].delete();
                            String path = rootPath+"/alarm/" + logInfoFormat.format(new Date(System.currentTimeMillis())) + ".txt";
                            if (handler!=null){
                                handler.setPath(path);
                            }
                        }else{
                            if (handler!=null){
                                handler.setPath(files[files.length-1].getAbsolutePath());
                            }
                        }
                    }else{
                        if (files[files.length-1].length()>2*1024*1024){
                            String path = rootPath+"/alarm/" + logInfoFormat.format(new Date(System.currentTimeMillis())) + ".txt";
                            if (handler!=null){
                                handler.setPath(path);
                            }
                        }else{
                            if (handler!=null){
                                handler.setPath(files[files.length-1].getAbsolutePath());
                            }
                        }
                    }
                }
            }
        }).start();
    }
    public static void  print(AlarmInfo alarmInfo){
        if (handler!=null){
            handler.print(alarmInfo);
        }
    }
    private static SimpleDateFormat logInfoFormat = new SimpleDateFormat("yyyy-MM-dd");
}
